﻿#region CREDIT & LICENSE
/*******************************************
 * CREDIT: 
 * http://www.ROMPROS.com 
 * Tremendously helpful website, also would
 * like to give special thanks to:
 * 
 * Roverser - for the great effort on a c repository 
 * that most of this was ported from. http://code.google.com/p/rombot/
 * 
 * ROMeo - for the great amount of information shared
 * all across the rompros site. http://www.rompros.com/member.php?u=1901
 * 
 * 
 * LICENSE:
 *  
 * RomUI is WPF(Windows Presentation Foundation) user interface for interacting with the 
 * RomAPI used to manipulate the Runes Of Magic game client. 
 * 
 * Copyright (C) 2010  digitalxarts@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *******************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RomAPI;
using RomAPI.Objects;
namespace RomUI
{
    /// <summary>
    /// Interaction logic for Radar.xaml
    /// </summary>
    public partial class Radar : UserControl
    {
        public RadarUnit[] radarUnits;
        public RomPlayer Player;
        public RomWorld World;
        public static double RadarWidth;
        public static double RadarHeight;
        public static double RadarScale;
        public static string FilterName;
        public static Dictionary<uint, string> ObjectDictonary;
        public Radar()
        {
            InitializeComponent();
        }
        private void Page_Initialized(object sender, EventArgs e)
        {
            try
            {
                RadarWidth = this.recPlayerPosition.Margin.Left;
                RadarHeight = this.recPlayerPosition.Margin.Top;
                RadarScale = .3;
                int b = RomClientManager.Clients.Count;
                RomClient client = (RomClient)RomClientManager.Clients[0];

                Player = client.Player;
                World = client.World;
                radarUnits = new RadarUnit[World.MaximumSize];

                for (int i = 0; i < World.MaximumSize; i++)
                {
                    radarUnits[i] = new RadarUnit(i, World.WorldObjects[i]);
                    radarUnits[i].Visibility = System.Windows.Visibility.Hidden;
                    World.OnUpdate += radarUnits[i].Update;
                    gridPage.Children.Add(radarUnits[i]);
                }
                World.OnUpdate += new RomWorldUpdateHandler(World_OnUpdate);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Rom must be running.", "Radar Error", MessageBoxButton.OK, MessageBoxImage.Error);
                //throw ex;
                //need to find better way to close this window.
            }
        }
        public void FilterByName(string objectName)
        {
            Radar.FilterName = objectName;
        }
        public delegate void RefreshHandler();
        protected void Refresh()
        {
            recPlayerPosition.RenderTransform = new RotateTransform(360 - Player.GetViewingAngle(), recPlayerPosition.Width / 2, recPlayerPosition.Height / 2);
            lblPlayerCoords.Content = Player.X + ", " + Player.Y;
        }
        protected void World_OnUpdate(RomWorld world)
        {
            this.recPlayerPosition.Dispatcher.Invoke(new RefreshHandler(Refresh));
        }

        private void sliderZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Radar.RadarScale = sliderZoom.Value;
            if (lblZoomPercent != null)
                lblZoomPercent.Content = "%" + (sliderZoom.Value * 100);
        }
    }
}
